]> git.neil.brown.name Git - wiggle.git/blob - split.c
New release: v1.3
[wiggle.git] / split.c
1 /*
2  * wiggle - apply rejected patches
3  *
4  * Copyright (C) 2003-2013 Neil Brown <neilb@suse.de>
5  * Copyright (C) 2014-2020 Neil Brown <neil@brown.name>
6  *
7  *
8  *    This program is free software; you can redistribute it and/or modify
9  *    it under the terms of the GNU General Public License as published by
10  *    the Free Software Foundation; either version 2 of the License, or
11  *    (at your option) any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License
19  *    along with this program.
20  *
21  *    Author: Neil Brown
22  *    Email: <neil@brown.name>
23  */
24
25 /*
26  * Split a stream into words or lines
27  *
28  * A word is one of:
29  *    string of [A-Za-z0-9_]
30  *    or string of [ \t]
31  *    or single char (i.e. punctuation and newlines).
32  *
33  * A line is any string that ends with \n
34  *
35  * As a special case to allow proper aligning of multiple chunks
36  * in a patch, a word starting \0 will include 20+ chars with a newline
37  * second from the end.
38  *
39  * We make two passes through the stream.
40  * Firstly we count the number of item so an array can be allocated,
41  * then we store start and length of each item in the array
42  *
43  */
44
45 #include        "wiggle.h"
46 #include        <stdlib.h>
47 #include        <ctype.h>
48 #include        <stdlib.h>
49
50 #include "ccan/hash/hash.h"
51
52 static int wiggle_split_internal(char *start, char *end, int type,
53                           struct elmnt *list)
54 {
55         int cnt = 0;
56
57         while (start < end) {
58                 char *cp = start;
59                 char *cp2;
60                 int prefix = 0;
61
62                 if ((type & ByWord) && (type & IgnoreBlanks))
63                         while (cp < end &&
64                                (*cp == ' ' || *cp == '\t')) {
65                                 prefix++;
66                                 cp++;
67                         }
68                 start = cp;
69
70                 if (*cp == '\0' && cp+19 < end) {
71                         /* special word */
72                         cp += 19;
73                         cp += strlen(cp) + 1;
74                 } else
75                         switch (type & ByMask) {
76                         case ByLine:
77                                 while (cp < end && *cp != '\n')
78                                         cp++;
79                                 if (cp < end)
80                                         cp++;
81                                 break;
82                         case ByWord:
83                                 if (*cp == ' ' || *cp == '\t') {
84                                         do
85                                                 cp++;
86                                         while (cp < end
87                                                && (*cp == ' '
88                                                    || *cp == '\t'));
89                                 } else if ((type & WholeWord) ||
90                                            isalnum(*cp) || *cp == '_') {
91                                         do
92                                                 cp++;
93                                         while (cp < end
94                                                && (((type & WholeWord)
95                                                     && *cp != ' ' && *cp != '\t'
96                                                     && *cp != '\n')
97                                                    || isalnum(*cp)
98                                                    || *cp == '_'));
99                                 } else
100                                         cp++;
101                                 break;
102                         }
103                 cp2 = cp;
104                 if ((type & ByWord) && (type & IgnoreBlanks) &&
105                     *start && *start != '\n')
106                         while (cp2 < end &&
107                                (*cp2 == ' ' || *cp2 == '\t' || *cp2 == '\n')) {
108                                 cp2++;
109                                 if (cp2[-1] == '\n')
110                                         break;
111                         }
112                 if (list) {
113                         list->start = start;
114                         list->len = cp-start;
115                         list->plen = cp2-start;
116                         list->prefix = prefix;
117                         if (*start)
118                                 list->hash = hash(start, list->len, 0);
119                         else
120                                 list->hash = atoi(start+1);
121                         list++;
122                 }
123                 cnt++;
124                 start = cp2;
125         }
126         return cnt;
127 }
128
129 struct file wiggle_split_stream(struct stream s, int type)
130 {
131         int cnt;
132         struct file f;
133         char *c, *end;
134
135         if (!s.body) {
136                 f.list = NULL;
137                 f.elcnt = 0;
138                 return f;
139         }
140         end = s.body+s.len;
141         c = s.body;
142
143         cnt = wiggle_split_internal(c, end, type, NULL);
144         f.list = wiggle_xmalloc(cnt*sizeof(struct elmnt));
145
146         f.elcnt = wiggle_split_internal(c, end, type, f.list);
147         return f;
148 }